<?php

class SystemInfoController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations

		);
	}
	public function init(){
		Yii::app()->theme = 'admincp';
		$this->layout = '//layouts/admincp';
	}
	public function actionIndex()
	{
		$model = SystemInfo::model()->findByPk(1);
		if($model === null)
			throw new CHttpException('404','Không tìm thấy trang');
			
		if(!empty($_POST['SystemInfo'])){
			$model->attributes = $_POST['SystemInfo'];
			
			$fileUpload = CUploadedFile::getInstance($model,'logo');
			if($fileUpload != null){
				$fileName = $fileUpload->name;
				if(file_exists(Yii::getPathOfAlias('webroot').'/upload/images/'.$fileName)){
					$fileName = explode('.',$fileName);
					$fileName = $fileName[0].'_'.time().'.'.$fileName[count($fileName)-1];
				}
				$model->logo = $fileName;
			}
			//dump($fileName);
			if($model->save())
			{
				if($fileUpload != null){
					$fileUpload->saveAs(Yii::getPathOfAlias('webroot').'/upload/images/'.$fileName);
				}
				Yii::app()->user->setFlash('success', "Cập nhật thành công.");
				$this->redirect($_SERVER['HTTP_REFERER']);
			}
		}
		$this->pageTitle = "Thông tin hệ thống";
		$this->render('index',array('model'=>$model,'label'=>$model->attributeLabels()));
	}
	public function actionSlideHome()
	{
		$model = HomeSlides::model()->findAll(array('order'=>'id DESC'));
		
		if(!empty($_POST['HomeSlides'])){
			$model = new HomeSlides;
			$this->PostSave($_POST['HomeSlides'],$model);
		}
		$this->pageTitle = "Slide trang chủ";
		$this->render('slide_home',array('model'=>$model));
	}
	
	public function actionUpdateSlideHome($id = null){
		$model = HomeSlides::model()->findByPk($id);
		if($model !== null && !empty($_POST['HomeSlides']))
		{
			$this->PostSave($_POST['HomeSlides'],$model);
		}else
			throw new CHttpException('404','Không tìm thấy trang.');
		
		
	}
	
	public function PostSave($data,$model){
		$model->attributes = $data;
		$fileUpload = CUploadedFile::getInstance($model,'image');
		if($fileUpload != null){
			$fileName = $fileUpload->name;
			if(file_exists(Yii::getPathOfAlias('webroot').'/upload/slides/'.$fileName)){
				$fileName = explode('.',$fileName);
				$fileName = $fileName[0].'_'.time().'.'.$fileName[count($fileName)-1];
			}
			$model->image = $fileName;
			$model->created = time();
		}
		if($model->save())
		{
			if($fileUpload != null){
				$fileUpload->saveAs(Yii::getPathOfAlias('webroot').'/upload/slides/'.$fileName);
			}
			if($model->isNewRecord){
				Yii::app()->user->setFlash('success', "Thêm mới thành công.");
			}else{
				Yii::app()->user->setFlash('success', "Cập nhật thành công.");
			}
		}else{
			Yii::app()->user->setFlash('error', "Hình ảnh không được trống.");
		}
		$this->redirect(PIUrl::createUrl('/admincp/systemInfo/slideHome'));
	}
	
	public function actionDeleteSlideHome($id = null){
		$model = HomeSlides::model()->findByPk($id);
		if($model === null)
			throw new CHttpException('404','Không tìm thấy trang.');
			
		$fileName = $model->image;
		if($model->delete()){
			if($fileName != '' && file_exists(Yii::getPathOfAlias('webroot').'/upload/slides/'.$fileName)){
				unlink(Yii::getPathOfAlias('webroot').'/upload/slides/'.$fileName);
			}
			Yii::app()->user->setFlash('success', "Xóa thành công.");
		}else{
			Yii::app()->user->setFlash('error', "Xóa thất bại.");
		}
		$this->redirect(PIUrl::createUrl('/admincp/systemInfo/slideHome'));
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}